import collections
import re
import sys

from pymysql.cursors import Cursor

sys.path.extend(["F:/ML/BNC", "D:/BNC", "/www/wwwroot/BNC"])
from DataBase.DBPoolObject import DB_POOL
from DataBase.DatabaseConsts import StockCalendarDB, CodeTagDateTableName, StockTagRenameTableName, AStockTagDB
from StockRelation.Funcc import tag_suffix_remove

DB_pool = DB_POOL.get_db_pool(db=AStockTagDB,
                              )

connection = DB_pool.connection()
cursor = connection.cursor()

# DB_pool = DB_POOL.get_db_pool(db=AStockTagDB,
#                               host='localhost',
#                               passwd='h303567469'
#                               )

MODIFIED = 1
NOT_MODIFIED = -1
DISPOSED = 1
NOT_DISPOSED = -1


def dispose_some_tag():
    """ 获取全部的标签和已知的标签，废除部分不需要的标签 """
    """ 标签数据库对象 from to disposed parsed 
        废弃的
    """
    pass


def combine_tags(tags) -> dict:
    # 保留空格 保留原有顺序，去除部分后缀 保持唯一性（除空行）
    # end_pat = re.compile(".*(大涨|涨价|系|版块|板块|概念|概念股)$")
    rename_dic = dict()
    min_len = 1
    max_len = 10
    nw_tags_split_b = []
    for tg_ in tags:
        if tg_ == '':
            nw_tags_split_b.append(tg_)
        elif len(tg_) >= max_len or len(tg_) <= min_len:
            continue
        else:
            _renamed = machine_base_parse_tag(tg_)
            # _renamed = re.sub("(大涨|涨价|系|版块|板块|概念|概念股)$", '', tg_)
            nw_tags_split_b.append(_renamed)
            rename_dic[tg_] = _renamed
    tgs = []
    appear_di = dict()
    for nw_ in nw_tags_split_b:
        if nw_ == '':
            tgs.append(nw_)
            continue
        if nw_ not in appear_di:
            tgs.append(nw_)
        appear_di[nw_] = True
    nw_tags_split_b = tgs
    print("origin len : {} modify len :{}".format(len(tags), len(nw_tags_split_b)))
    print("parsed_tag:{}".format(nw_tags_split_b))
    return rename_dic


def save_tags_map_2_db(tag_dic: dict):
    di = {"st_db": "stocktag", "tr_tb": "tag_rename"}
    replace_sql = "REPLACE INTO {st_db}.{tr_tb} (`from`,`to`) VALUES (%s,%s)".format(**di)

    try:
        cursor.executemany(replace_sql, [(tg_, tag_dic[tg_],) for tg_ in tag_dic])
        connection.commit()
    finally:
        cursor.close()
        connection.close()


def combine_two_by_head(tags):
    """ 用头部进行相互的结合 如阿里 阿里巴巴， 将阿里巴巴归到`阿里`中
        需要保证原有的tags的顺序，并且不破坏原有的结构
    """
    _new_tags = []
    head_dic = collections.OrderedDict()
    # 结合实现NavigableMap的结构
    head_dic[''] = []
    for tg_ in tags:
        has_key = False

        for head_ in head_dic:
            if head_.strip() == '':
                continue
            if tg_.startswith(head_):
                head_dic[head_].append(tg_)
                has_key = True
                break
        if not has_key:
            head_dic[tg_] = [tg_]

    s1 = ''
    for head_ in head_dic:
        childs_ = head_dic[head_]
        assert len(childs_) > 0, "head_ {} has no child".format(head_)
        if len(childs_) == 1:
            s1 += childs_[0]
        else:
            s1 += '\n'
            s1 += head_ + '\n'
            s1 += '/\n'
            for child_ in childs_:
                s1 += child_
                s1 += '\n'

        s1 += '\n'
    print(s1)
    return s1


def augment_tag_from_db():
    """ 从code_tag_date中获取新的标签，按照from == to 的格式直接插入到数据库中去 modified 为-1代表 未修改"""
    # warning 这个方法需要连接到本地的数据库进行测试，
    # 等于from
    # def insert_many_dict():
    #     values_dict = [{'from':'test1','to':'test1','modified':'-1'},{'from':'test1','to':'test1','modified':'-1'}]
    #     REPLACE_SQL = "REPLACE INTO {tr_tb}".format(**{"tr_tb":StockTagRenameTableName})+" (`from`,`to`,modified) VALUES ({from},{test1},{to})"
    #     cursor.executemany(REPLACE_SQL,values_dict)
    #     pass
    #
    # insert_many_dict()
    di = {'ctd_tb': CodeTagDateTableName}
    select_tag_from_ctd = "select distinct tag from {ctd_tb};".format(**di)
    cursor.execute(select_tag_from_ctd)
    tags_from_ctd = [r['tag'] for r in cursor.fetchall()]
    # tag可能存在于to或from中， => 若 to == tag 可以直接排除， 若from == tag AND NOT modified
    # 如果标签在to中出现，可以直接插入为tag == to , from 中未出现 修改为from == tag not modified ， from 中出现
    #  to 中出现需要插入 to == to   ||  from 中出现都可以不进行修改 || from中未出现标记为NOT MODIFIED  from == to
    di = {"tr_tb": StockTagRenameTableName, "mod": MODIFIED}
    select_all_parsed_sql = "select `from`,`to` from {tr_tb} where modified = {mod};".format(**di)
    cursor.execute(select_all_parsed_sql)

    has_to_dic = dict()
    not_existed_from_dic = dict()
    from_set = set()
    to_set = set()
    for r_ in cursor.fetchall():
        fr_ = r_['from']
        to_ = r_['to']
        from_set.add(fr_)
        to_set.add(to_)

    # 修改后缀名的东西也不能 替换掉已有的数据信息 标签命名时不应该改变已有的后缀信息
    for tag_ctd_ in tags_from_ctd:
        tag_post_processed = machine_base_parse_tag(tag_ctd_)
        if tag_post_processed in to_set:
            # 保证成功
            has_to_dic[tag_ctd_] = tag_post_processed
        elif tag_ctd_ in from_set:
            pass
        else:
            not_existed_from_dic[tag_ctd_] = tag_post_processed

    # 需要进行插入 使用REPLACE 插入需要观察是否存在
    # 对于额外的属性不进行操作
    # 可以直接完成映射的。
    REPLACE_SQL = "REPLACE INTO {tr_tb} (`from`,`to`,modified) VALUES (%s,%s,%s)".format(**di)
    cursor.executemany(REPLACE_SQL, [(t_, has_to_dic[t_], MODIFIED,) for t_ in has_to_dic])
    connection.commit()

    # for tag_ in not_existed_from_dic:
    REPLACE_SQL = "REPLACE INTO {tr_tb} (`from`,`to`,modified,dispose) VALUES (%s,%s,%s,%s)".format(**di)
    cursor.executemany(REPLACE_SQL,
                       [(t_, not_existed_from_dic[t_], NOT_MODIFIED, DISPOSED) for t_ in not_existed_from_dic])
    connection.commit()


def machine_base_parse_tag(tag_post_processed):
    # 去除后缀
    while tag_post_processed != re.sub("(大涨|涨价|系|版块|板块|概念|概念股|公司|集团)$", '', tag_post_processed):
        tag_post_processed = re.sub("(大涨|涨价|系|版块|板块|概念|概念股|公司|集团)$", '', tag_post_processed)
    return tag_post_processed


def disable_tag():
    pass


def detection_unrecord_tag():
    """ 将复盘的数据与已有的标签转化进行比较，找出不通过的，标记是否废弃
        modified -1 未修改 1 已修改
        update后需要保持对应
    """
    # 对modified 修改

    pass



def generate_combine_tag():
    """ 优先保存存在化分中的字符串
        进行后缀去除
        结果不能包含同后缀的词
    """
    file_path = ''
    # 对读入的数据进行处理
    lines = []
    with open("D:/对标签进行归类20200115改.txt", mode='r', encoding='utf8') as f:
        lines = f.readlines()
    # 读入一个/ 进入append模式 读入到空行 进入普通模式
    need_append_childs = False
    normal = True
    ends_with_pat = re.compile(".*(/)$")
    tag_map = dict()  # {A : [B, priority]} 0 max, 1 min
    father = ''
    total_tag_num_i = 0
    total_tag_not_combine_i = 0
    for line_ in lines:
        line_ = line_.replace("\n", "")
        if line_.strip() != '':
            total_tag_not_combine_i += 1
        # blank
        if ends_with_pat.match(line_):
            normal = False
            father = re.sub("(/)$", "", line_)
            total_tag_num_i += 1
            total_tag_not_combine_i -= 1
            continue
        if normal:
            renamed_ = line_
            renamed_ = tag_suffix_remove(renamed_)
            if line_ not in tag_map or (line_ in tag_map and 1 == tag_map[line_][1]):
                # if renamed_ in tag_map  and 1 == tag_map[line_][1] and not renamed_.strip() == '':
                #     print("重复替换 :{} ".format(renamed_))
                tag_map[line_] = [renamed_, 1]
                if line_.strip() != '':
                    total_tag_num_i += 1
        else:
            if line_.strip() == '':
                normal = True
            else:
                if line_ in tag_map:
                    print("重复替换 :{} ".format(line_))
                tag_map[line_] = [father, 0]

    # 将所有关联到具体城市的标签，改为引用自己 || 添加城市的标签
    cities_tag_set = {'深圳', '北京', '吉林', '江苏', '辽宁', '广东', '安徽', '四川', '浙江', '湖南', '河北', '新疆', '山东', '河南', '山西', '江西',
                      '青海', '湖北', '内蒙', '海南', '重庆', '陕西', '福建', '广西', '天津', '云南', '贵州', '甘肃', '宁夏', '黑龙江', '上海', '西藏'}
    for tag_f_ in list(tag_map.keys()):
        tag_map[tag_f_] = tag_map[tag_f_][0]

    for citie_tag in cities_tag_set:
        tag_map[citie_tag] = citie_tag
    print(tag_map)
    print("total tag : {}, not combine:{}".format(total_tag_num_i, total_tag_not_combine_i))
    return tag_map


if __name__ == '__main__':
    # augment_tag_from_db()
    exit()
    # tags = []
    # nts = []
    # with open("D:/去除len大于等于10的并且去除概念、系、版块后缀.txt", mode='r', encoding='utf8') as f:
    #     tags = f.readlines()
    # for tg_ in tags:
    #     nts.append(tg_.replace("\n", ''))
    # combine_two_by_head(nts)
    # tag_map = generate_combine_tag()
    # 需要去除后缀

    # new_tag_dic = combine_tags(nts)
    # new_tag_dic = dict()
    # for tg_ in tags:
    #     new_tag_dic[tg_] = tg_
    # save_tags_map_2_db(tag_map)
